home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: help with pi algorithm
- Date: 25 Jan 1996 05:29:41 GMT
- Organization: News & Observer Public Access
- Message-ID: <4e74g5$ot1@castle.nando.net>
- References: <0fc_9601240111@csource.blaze.net.au>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: 152.52.37.91
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <0fc_9601240111@csource.blaze.net.au>, Andrew.Nesbit@f396.n634.z3.fidonet.org (Andrew Nesbit) writes:
- >Hi all!
- >
- >I am a newbie C programmer trying to write an algorithm to work out the
- >value of pi fairly accurately. I have written the following code, but I'm
- >not sure whether it's considered 'nasty' programming, or whether it could
- >be written to run more quickly.
- >
- >#include <stdio.h>
- >main()
- >{
- > long double pi = 0;
- > long int = count;
- > for (count = 1; count <= 300000; count += 4) {
- > pi += 4.0 / count; pi -= 4.0 / (count + 2);
- > }
- > printf("Value of pi is approx %.19Lf)", pi);
- > return 0;
- >}
- >
- >Any comments please???
-
- You appear to be using the famous Leibniz Series from the
- 17th century. It works but is a bit slow.
-
- Try the Tamura-Kanada Algorithm and watch this sucker
- converge:
-
- double tamura( int n )
- {
- double a = 1, b = 1 / sqrt(2), c = .25, p2 = 1, y;
-
- while( --n > 0 )
- {
- y = a;
- a = (a + b) / 2;
- b = sqrt(b * y);
- y = a - y;
- c -= p2 * y * y;
- p2 *= 2;
- }
-
- b += a;
- return b * b / c / 4;
- }
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-